﻿Public Class MyBorder
    Inherits Border

    Private m_eventcount As Integer = 0
    Private m_original_child As UIElement

    Private m_message_text As TextBox

    '    Private m_major As Boolean = False

    Public Shared MajorProperty As DependencyProperty = _
        DependencyProperty.Register("Major", _
                                    GetType(Boolean), _
                                    GetType(MyBorder), _
                                    New PropertyMetadata(False, New PropertyChangedCallback(AddressOf OnMajorChanged)))

    Public Property Major() As Boolean
        Get
            Return CType(GetValue(MajorProperty), Boolean)
        End Get
        Set(ByVal value As Boolean)
            SetValue(MajorProperty, value)
        End Set
    End Property

    Private Shared Sub OnMajorChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        If e.OldValue = e.NewValue Then Return

        Dim b As MyBorder = CType(d, MyBorder)
        Dim p As Panel = CType(b.Parent, Panel)
        If p Is Nothing Then Return

        Dim lastmajor As MyBorder = Nothing
        If e.NewValue Then ' make this major, change current major MyBorder to False
            For Each other As MyBorder In p.Children.OfType(Of MyBorder)()
                If other IsNot b AndAlso other.Major Then
                    lastmajor = other
                End If
            Next
        Else ' make minor
        End If
        If lastmajor IsNot Nothing Then
            SwapContent(b, lastmajor)
            lastmajor.Major = False
        End If
        'b.SetVisual()
        'If lastmajor IsNot Nothing Then
        '    SwapPosition(b, lastmajor)
        '    lastmajor.Major = False
        'End If
    End Sub

    Public Shared Sub SwapContent(ByVal b1 As MyBorder, ByVal b2 As MyBorder)
        Dim c1 As UIElement = b1.Child
        Dim c2 As UIElement = b2.Child
        b1.Child = Nothing
        b2.Child = Nothing
        c1.InvalidateVisual()
        c2.InvalidateVisual()
        b1.Child = c2
        b2.Child = c1
        Dim e As FrameworkElement = b1.Parent
        e.InvalidateVisual()
    End Sub

    Public Shared Sub SwapPosition(ByVal b1 As MyBorder, ByVal b2 As MyBorder)
        Dim dims() As Double = New Double() {Canvas.GetLeft(b1), Canvas.GetTop(b1), b1.Width, b1.Height}
        SetDims(b1, b2)
        SetDims(b2, dims)
    End Sub
    Public Shared Function GetDims(ByVal b As MyBorder) As Double()
        Return New Double() {Canvas.GetLeft(b), Canvas.GetTop(b), b.Width, b.Height}
    End Function
    Public Shared Sub SetDims(ByVal b As MyBorder, ByVal b1 As MyBorder)
        SetDims(b, GetDims(b1))
    End Sub

    Public Shared Sub SetDims(ByVal b As MyBorder, ByVal dims() As Double)
        Dim content As FrameworkElement = b.Child
        b.Child = Nothing
        Dim stack As New StackPanel
        Dim txt As New TextBox
        txt.Text = "hello"
        stack.Children.Add(txt)
        stack.Children.Add(content)
        b.Child = stack
        Canvas.SetLeft(b, dims(0))
        Canvas.SetTop(b, dims(1))
        content.Width = dims(2)
        content.Height = dims(3)
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
        m_eventcount += 1
        If e.ClickCount = 2 Then
            m_message_text.Text = "OnMouseDown: doubleclick: " & m_eventcount & " major: " & Major
            Me.Major = True
        Else
            m_message_text.Text = "OnMouseDown: " & m_eventcount & " major: " & Major
        End If
        e.Handled = False
    End Sub

    Private Sub MyBorder_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
        m_message_text = New TextBox
        m_message_text.Text = "initialized..."

        m_original_child = Me.Child
        Me.Child = Nothing

        Dim stack As New StackPanel
        stack.Children.Add(m_original_child)
        stack.Children.Add(m_message_text)

        Dim insideborder As New Border
        insideborder.BorderBrush = Brushes.Green
        insideborder.BorderThickness = New Thickness(2)
        insideborder.Child = stack

        Me.Child = insideborder

        Me.SetVisual()

    End Sub

    Private Sub SetVisual()
        Me.BorderThickness = New Thickness(3, 10, 3, 3)
        If Major Then
            Me.BorderBrush = Brushes.Red
            Me.Background = Brushes.Red
            'Canvas.SetTop(Me, 0)
            'Canvas.SetLeft(Me, 0)
        Else
            Me.BorderBrush = Brushes.Yellow
            Me.Background = Brushes.Yellow
        End If
        'Me.Invalidate
    End Sub

    Private Sub MyBorder_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    End Sub
End Class
